home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / netzwerk / tcp-ip / usno / time_chk / time_chk.c
C/C++ Source or Header  |  1996-02-26  |  4KB  |  133 lines

  1. /* time_chk.c    R.E. Schmidt, TS, US Naval Observatory, March 1992
  2.  *
  3.  * Check local system clock against the timer server host.
  4.  * This program requests a service called "timesrv".  In order for
  5.  * it to function, an entry for it needs to exist in the
  6.  * /etc/services file.  The port address for this service can be
  7.  * any port number that is likely to be unused, such as 22375,
  8.  * for example.  The host on which the server will be running
  9.  * must also have the same entry (same port number) in its
  10.  * /etc/services file.
  11.  *
  12.  * The name of the system to which the requests will be sent is given
  13.  * as a parameter to the command.
  14.  */
  15.  
  16.  
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <stdio.h>
  21. #include <netdb.h>
  22. #include <time.h>
  23.  
  24. #ifdef AMIGA
  25.   #include <stdlib.h>
  26.   #include <string.h>
  27. #endif
  28.  
  29. #define LEN     sizeof(long)
  30.  
  31. int s;                          /* connected socket descriptor */
  32. struct hostent *rh;             /* pointer to host info for remote host */
  33. struct servent *sport;          /* pointer to service information */
  34. long timevar;                   /* contains time returned by time() */
  35. char *(ctime)();                /* declare time formatting routine */
  36. struct sockaddr_in pad_in;      /* for peer socket address */
  37.  
  38. void main(argc, argv)
  39.   int argc;
  40.   char *argv[];
  41. {
  42.   auto int xcode = 1;
  43.   auto long tsecs;
  44.   auto char myname[40];
  45.  
  46.   if (argc == 2)
  47.   {
  48.     /* Get local host name
  49.      */
  50.     gethostname(myname, 40);
  51.  
  52.     /* clear out address structures
  53.      */
  54.     memset((char *) &pad_in, 0, sizeof(struct sockaddr_in));
  55.  
  56.     /* Set up the peer address to which we will connect.
  57.      */
  58.     pad_in.sin_family = AF_INET;
  59.  
  60.     /* Get the host information for the hostname that the user passed in.
  61.      */
  62.     if ((rh = gethostbyname(argv[1])) != NULL)
  63.     {
  64.       pad_in.sin_addr.s_addr = ((struct in_addr *) (rh->h_addr))->s_addr;
  65.  
  66.       /* Find the information for the "timesrv" server in order to get the needed
  67.        * port number.
  68.        */
  69.       if ((sport = getservbyname("timesrv", "tcp")) != NULL)
  70.       {
  71.         pad_in.sin_port = sport->s_port;
  72.  
  73.         /* Create the socket.
  74.          */
  75.         if ((s = socket(AF_INET, SOCK_STREAM, 0)) != -1)
  76.         {
  77.           /* Try to connect to the remote server at the address which was just built
  78.            * into peeraddr.
  79.            */
  80.           if (connect(s, (struct sockaddr const *) &pad_in, sizeof(struct sockaddr_in)) != -1)
  81.           {
  82.             if (recv(s,(unsigned char *) &tsecs, LEN, 0) != -1)
  83.             {
  84.               tsecs = ntohl(tsecs);
  85.  
  86.               /* Get local time
  87.                */
  88.               time(&timevar);
  89.  
  90.               /* Print message indicating completion of task.
  91.                */
  92.               printf("Local Time.....: %24.24s at %s\n",ctime(&timevar),myname );
  93.               printf("Remote Time....: %24.24s at %s\n",ctime(&tsecs  ),argv[1]);  /* GMT string */
  94.               printf("Local - Server.: %d secs\n", timevar - tsecs);
  95.  
  96.               /* return OK on exit.
  97.                */
  98.               xcode = 0;
  99.             }
  100.             else
  101.             {
  102.               perror(argv[0]);
  103.               fprintf(stderr, "%s: error reading result\n", argv[0]);
  104.             }
  105.           }
  106.           else
  107.           {
  108.             perror(argv[0]);
  109.             fprintf(stderr, "%s: unable to connect to remote\n", argv[0]);
  110.           }
  111.         }
  112.         else
  113.         {
  114.           perror(argv[0]);
  115.           fprintf(stderr, "%s: unable to create socket\n", argv[0]);
  116.         }
  117.       }
  118.       else
  119.         fprintf(stderr, "%s: timesrv not found in /etc/services\n", argv[0]);
  120.     }
  121.     else
  122.       fprintf(stderr, "%s: %s not found in /etc/hosts\n", argv[0], argv[1]);
  123.   }
  124.   else
  125.   {
  126.     fprintf(stderr, "Usage: %s <remote host>\n", argv[0]);
  127.     fprintf(stderr, "Hosts: tick.usno.navy.mil\n");
  128.     fprintf(stderr, "       tock.usno.navy.mil\n");
  129.   }
  130.  
  131.   exit(xcode);
  132. }
  133.